﻿using MongoDB.Driver;
using Sustainalytics.StorageSnapshot.AWS;
using Sustainalytics.StorageSnapshot.BusinessLogic.Entities;
using Sustainalytics.StorageSnapshot.BusinessLogic.Enums;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.StorageSnapshot.BusinessLogic
{
    public class SnapshotManagement
    {
        protected IMongoClient _client;
        protected IMongoDatabase _database;

        public async void Start()
        {
            var x = GetSync();

            if (x.Status == LastSessionStatus.Finished)
            {
                if (DateTime.Now.DayOfYear == x.TimeEnd.Value.DayOfYear)
                {
                   var IsAvailableSnaphot = await EbsSnapshot.CheckSnapshotForVolume("vol-21a452ee");

                    if (!IsAvailableSnaphot)
                    {
                        //await EbsSnapshot.CreateSnapshotForVolume("vol-21a452ee");
                    }
                }
            }
            else
            {
                Console.WriteLine("Last sync not finished");
            }
           
        }

        public SyncLog GetSync()
        {
            _client = new MongoClient(ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString);
            _database = _client.GetDatabase("ClientDWMaster");

            var collection = _database.GetCollection<SyncLog>(typeof(SyncLog).Name);
            var lastSync = collection.AsQueryable()
                .OrderByDescending(sl => sl.TimeStart)
                .Select(sl => sl)
                .FirstOrDefault();

            return lastSync;

        }
    }
}
